我返回集合的 Asp.net mvc 操作生成包含 system.linq.Enumerable 的 url,如何刪除它們 (My Asp.net mvc actions that return collection generate urls containing system.linq.Enumerable, how to remove them)


問題描述

我返回集合的 Asp.net mvc 操作生成包含 system.linq.Enumerable 的 url,如何刪除它們 (My Asp.net mvc actions that return collection generate urls containing system.linq.Enumerable, how to remove them)

I have an mvc application that basically generates views which are bascially collections but collections of a particular type..so i.e. 

IEnumerable<IType>

Now my urls contain stuff like this

www.site/home/section/param?System.Linq.Enumerable%.....

I want to remove anything after section.

I have tried the routeMap but have been unable to ommit the system.linq etc. Any ideas or help please

Here is my action method

public ActionResult Whatever(IEnumerable<IType> whatever)
        {
            return View(whatever);
        }

參考解法

方法 1:

You can't pass around collections like that in the URL, you shouldn't add them to the RouteValueDictionary when you redirect. Just send some basic information that can help you get what you need in the action you are redirecting to.

Edit: Based on your code, we can see that your action method takes an IEnumerable, so wherever you are calling this action, you're passing it one. You can't do that, you'll have to generate your list in the action method. Try something like:

public ActionResult Whatever()
{
    List<IType> whatever = new List<IType>();

    //populate your list here, then we can return it

    return View(whatever);
}

方法 2:

The question above is a very interesting and took me a while and has go back to my previously written mvc app. The answer is simple but not obvious. 

And it seems in the current mvc3 even if you pass the object as anonymous type mapping your view to your view model, it always works out the collectiontype and somehow appends that to the url in this case it was appending whole case to the url as

http://www.website.com/param=system.linq.enumerable.where.select..

any way the correct way is to wrap it in a routevaluedictionary 

new RouteValueDictionary(new { controller = Constants.HOMECONTROLLER, action = Constants.APPLYAPP }));

and if you are passing any stuff from one action to another use this

return new RedirectToRouteResult(Constants.DEFAULTROUTE,
                     new RouteValueDictionary(new { controller = Constants.HOMECONTROLLER, action = Constants.APPLYAPP }));

instead of RedirecttoAction because it seems this results in the url above.

Thanks MattyTommo for the help but its irrelevant in my case but it may be helpful for others. I cannot mark mattytommo's reply as answer at all because it actually misunderstood my request, perhaps my request wasnt correct but also misguided me.

(by user182630mattytommouser182630)

參考文件

  1. My Asp.net mvc actions that return collection generate urls containing system.linq.Enumerable, how to remove them (CC BY-SA 3.0/4.0)

#url-rewriting #asp.net-mvc #C#






相關問題

靜態內容域的 IIS 重定向規則 (IIS redirect rules for static content domains)

IIS 重寫模塊重寫映射性能 (IIS Rewrite Module rewrite map performance)

我返回集合的 Asp.net mvc 操作生成包含 system.linq.Enumerable 的 url,如何刪除它們 (My Asp.net mvc actions that return collection generate urls containing system.linq.Enumerable, how to remove them)

如何將查詢參數修改為路徑文件夾名稱? (How to mod_rewrite a query parameter to a path folder name?)

如何使用 .htaccess 將動態 URL 更改為 SEO 友好 (how to change the dynamic URL to SEO friendly using .htaccess)

AngularJS 和 URL 重寫為 HTTPS (AngularJS and URL Rewrite to HTTPS)

mod-rewrite 結合了兩種不同的重寫規則 (mod-rewrite combine two different rewrite rules)

htaccess 刪除 .php 擴展名並重寫為 www (htaccess remove .php extension and rewrite to www)

初學者 Apache URL 重寫問題 (Beginner Apache URL Rewrite Question)

.net datapager + listview + Intelligencia.UrlRewriter (.net datapager + listview + Intelligencia.UrlRewriter)

jquery get / post請求的Apache url替換 (Apache url replacement for jquery get / post request)

在python中解析url以提取參數 (Parsing url in python to extract parameters)







留言討論